/**
 * 
 */
package com.example.radioclub.login.yahoo.tasks;

import com.example.radioclub.data.Constants;
import com.example.radioclub.login.yahoo.interfaces.IYahooAuthTokenTask;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;
import android.os.AsyncTask;
import android.util.Log;

/**
 * Task to get Yahoo Auth and Access Token but it is Auth mostly.
 * 
 * @author wizard
 *
 */
public class YahooAuthTokenTask extends AsyncTask<OAuthProvider, Void, String[]> {
	private final String TAG = Constants.APP_TAG + "Yahoo Auth Access Token";
	private OAuthConsumer consumer;
	private IYahooAuthTokenTask listener;
	
	public YahooAuthTokenTask(OAuthConsumer consumer, IYahooAuthTokenTask listener) {
		// TODO Auto-generated constructor stub
		this.consumer = consumer;
		this.listener = listener;
	}
	
	@Override
	protected String[] doInBackground(OAuthProvider... params) {
		// TODO Auto-generated method stub
		if(params.length == 1) {
			if(params[0] != null) {
				try {
					params[0].retrieveAccessToken(consumer, "yahoo-callback://local?");
					return new String[] { this.consumer.getToken(), this.consumer.getTokenSecret() }; 
				} catch (OAuthMessageSignerException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					Log.i(TAG, e.getMessage());
				} catch (OAuthNotAuthorizedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					Log.i(TAG, e.getMessage());
				} catch (OAuthExpectationFailedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					Log.i(TAG, e.getMessage());
				} catch (OAuthCommunicationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					Log.i(TAG, e.getMessage());
				}
			}
		}
		return null;
	}
	
	@Override
	protected void onPostExecute(String[] result) {
		// TODO Auto-generated method stub
		this.listener.onAuthAccessTokenAcquired(result);
		super.onPostExecute(result);
	}
}
